﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class District
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}{2}", !string.IsNullOrEmpty(Region) ? string.Format("{0}-", Region) : string.Empty, Name, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public IList<Customer> Customers
        {
            get
            {
                return InternalCustomers.Where(customer => customer.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove Customer

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Customer AddCustomer(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customer = new Customer
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
            InternalCustomers.Add(customer);
            customer.IsDeleted = false;
            customer.ModifiedDate = DateTime.Now;
            customer.ModifiedByUser = modifiedUser;
            return customer;
        }

        /// <summary>
        /// </summary>
        /// <param name = "customer"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveCustomer(Customer customer, User modifiedUser)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalCustomers.Contains(customer))
            {
                customer.District = null;
                customer.ModifiedDate = DateTime.Now;
                customer.ModifiedByUser = modifiedUser;
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "District", "Name"));
            }
            Name = Name.Length > 64 ? Name.Substring(0, 64) : Name;
            Region = string.IsNullOrEmpty(Region) ? null : Region.Length > 32 ? Region.Substring(0, 32) : Region;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<Customer>) Customers).ForEach(customer =>
                                                     {
                                                         customer.District = null;
                                                         customer.ModifiedDate = DateTime.Now;
                                                         customer.ModifiedByUser = modifiedUser;
                                                     });
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}